package com.ruoyi.predict.service.impl;

import okhttp3.*;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import java.io.IOException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;

public class test {
    public static void main(String[] args) {
        // API地址和密钥（请替换为实际值）
        String url = "https://api.modelarts-maas.com/v1/chat/completions";
        String apiKey = "pMEYg6YUYvsADoCBCKpQi2o-AQYuffsxCGEHVyemh0aCAucWCF_uUE07gbYEDnNex_xfvyEykiWlwVQlekfo1g";  // 替换为实际API Key

        // 创建忽略SSL证书验证的OkHttpClient
        OkHttpClient client = createUnsafeOkHttpClient();

        // 构建请求JSON数据
        String jsonBody = "{" +
                "\"model\":\"openpangu-pro-moe-72b\"," +
                "\"messages\": [" +
                "    {\"role\": \"system\", \"content\": \"You are a helpful assistant.\"}," +
                "    {\"role\": \"user\", \"content\": \"你好\"}" +
                "  ]" +
                "}";

        // 构建请求体
        RequestBody requestBody = RequestBody.create(
                jsonBody,
                MediaType.parse("application/json; charset=utf-8")
        );

        // 构建请求
        Request request = new Request.Builder()
                .url(url)
                .addHeader("Content-Type", "application/json")
                .addHeader("Authorization", "Bearer " + apiKey)
                .post(requestBody)
                .build();

        // 发送请求并处理响应
        try (Response response = client.newCall(request).execute()) {
            // 打印状态码
            System.out.println("响应状态码: " + response.code());

            // 打印响应内容
            if (response.body() != null) {
                System.out.println("响应内容: " + response.body().string());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 创建忽略SSL证书验证的OkHttpClient（仅用于测试环境，生产环境不建议使用）
    private static OkHttpClient createUnsafeOkHttpClient() {
        try {
            // 创建信任所有证书的TrustManager
            final TrustManager[] trustAllCerts = new TrustManager[]{
                    new X509TrustManager() {
                        @Override
                        public void checkClientTrusted(X509Certificate[] chain, String authType) {}

                        @Override
                        public void checkServerTrusted(X509Certificate[] chain, String authType) {}

                        @Override
                        public X509Certificate[] getAcceptedIssuers() {
                            return new X509Certificate[0];
                        }
                    }
            };

            // 安装信任所有证书的SSL上下文
            final SSLContext sslContext = SSLContext.getInstance("SSL");
            sslContext.init(null, trustAllCerts, new SecureRandom());

            // 创建不验证主机名的HostnameVerifier
            final HostnameVerifier hostnameVerifier = (hostname, session) -> true;

            // 构建OkHttpClient
            return new OkHttpClient.Builder()
                    .sslSocketFactory(sslContext.getSocketFactory(), (X509TrustManager) trustAllCerts[0])
                    .hostnameVerifier(hostnameVerifier)
                    .build();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}
